$(function () {
    register();
    back_to_login();
    forget_password();
    apply_reset();
});


function register() {
    $('#register-start-button').click(() => {
        window.location.href = '/page/register.html';
    });
}

function back_to_login() {
    $('#login-start-button').click(() => {
        window.location.href = '/page/login.html';
    });
}

function forget_password() {
    $('#forget-keyword-button').click(() => {
        window.location.href = '/page/reset-user.html';
    });
}

function apply_reset() {
    $('#reset-form').on('submit', function (e) {
        e.preventDefault();
        if ($('#username').val() != '' && $('#password').val() != '') {
            $('#reset-confirm').attr('class', 'btn btn-warning');
            $('#reset-confirm').text('正在登陆..');
            $.ajax({
                url: '/reset-user-confirm',
                type: 'post',
                dataType: 'json',
                data: $(this).serialize(),
                success: function (data) {
                    console.log(data);
                    if (data['result'] == 1) { 
                        $('#reset-confirm').attr('class', 'btn btn-success');
                        $('#reset-confirm').text('重置成功，正在跳转');
                        setTimeout(`window.location.href='/page/login.html';`, 1500);
                    } else {
                        $('#reset-confirm').attr('class', 'btn btn-danger');
                        $('#reset-confirm').text('登陆失败，用户不存在或密码错误');
                    }
                }
            });
        } else {
            $('#reset-confirm').attr('class', 'btn btn-danger');
            $('#reset-confirm').text('重置失败，请完善信息后重试');
        }
    });
}